package thu.infinity.facebook;

import java.io.File;
import java.io.IOException;

import org.apache.thrift.TException;
import org.json.JSONException;
import org.json.JSONObject;

import thu.infinity.commons.AggregatorHandler;
import thu.infinity.commons.Task;
import thu.infinity.thrift.Constants;
import thu.infinity.thrift.FriendAlbum;

public class CacheProfileTask extends Task {
	String fbUserId;
	FriendAlbum frndAlbum;
	JSONObject pic;
	
	public CacheProfileTask(String fbUserId, FriendAlbum frndAlbum, JSONObject pic, AggregatorHandler handler) {
		this.fbUserId = fbUserId;
		this.frndAlbum = frndAlbum;
		this.handler = handler;
		this.pic = pic;
	}
	@Override
	public void execute() {
		// TODO Auto-generated method stub					
		try {
			frndAlbum.friend.profilePicId.id = pic.getString("id");
			String updatedTime = pic.getString("updated_time");
			frndAlbum.friend.profilePicId.updatedTime = updatedTime;
			String fn = Util.photoThumbName(frndAlbum.friend.profilePicId.id);
			FacebookUtil.cachePhoto(pic.getString("picture"), fn);
			File f = Util.newFile(fn);
			frndAlbum.friend.profilePicId.thumbSize = f.length();
			frndAlbum.friend.profilePicId.photoSize = Constants.PhotoUnavailable;
			fn = Util.friendAlbumName(fbUserId, frndAlbum.friend.id);
			f = Util.newFile (fn);
			FacebookUtil.writeFriendAlbumToFile(frndAlbum, f);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public boolean checkCondition() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	protected Task genNextTask() {
		// TODO Auto-generated method stub
		return null;
	}

}
